/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { HttpSrv } from 'lfun-bg-comp';
import { ImportParams, PageParams } from '..';

/* 关键信息 */
export interface SchoolKeyInfo {
  id: any;
  planning_school_id: any;
  school_name: string;
  borough: string;
  block: string;
  school_level: string;
  school_org_type: string;
  school_operation_type: string;
  school_operation_type_lv2: string;
  school_operation_type_lv3: string;
}
/* 查询规划校 */
export interface SearchSchool {
  school_name?: string;
  founder_type?: string[];
  founder_type_lv2?: string[];
  founder_type_lv3?: string[];
  borough?: string;
  block?: string;
  status?: string;
  school_code?: string;
  school_level?: string;
  process_code?: 'p_school_open' | 'p_school_close' | 'p_school_keyinfo_change';
}
/* 创建规划校 */
export interface CreateSchool {
  planning_school_id?: any;
  school_name?: string;
  school_no?: string;
  block?: string;
  borough?: string;
  school_level?: string;
}

/* 关闭规划校 */
export interface CloseSchool {
  school_id: string;
  action_reason: string;
  related_license_upload: string[];
}
// 基本信息、通讯信息、学校信息
export interface SchoolInfo {
  school: {
    id?: string;
    school_name?: string;
    school_short_name?: string;
    school_code?: string;
    create_school_date?: string;
    school_closure_date: string;
    founder_type?: string;
    founder_name?: string;
    urban_rural_nature?: string;
    school_operation_type?: string;
    school_org_form?: string;
    school_operation_type_lv2?: string;
    school_operation_type_lv3?: string;
    department_unit_number?: string;
    sy_zones?: string;
    historical_evolution?: string;
    status?: string;
  };
  school_communication: {
    id?: string;
    school_id?: string;
    postal_code?: string;
    fax_number?: string;
    email?: string;
    contact_number?: string;
    area_code?: string;
    long?: string;
    lat?: string;
    leg_repr_name?: string;
    party_leader_name?: string;
    party_leader_position?: string;
    adm_leader_name?: string;
    adm_leader_position?: string;
    loc_area?: string;
    loc_area_pro?: string;
    detailed_address?: string;
    related_license_upload?: string;
    school_web_url?: string;
  };
  school_eduinfo: {
    id?: string;
    school_id?: string;
    is_ethnic_school?: 'Y' | 'N';
    is_att_class?: 'Y' | 'N';
    att_class_type?: 'Y' | 'N';
    is_province_feat?: 'Y' | 'N';
    is_bilingual_clas?: 'Y' | 'N';
    minority_lang_code?: 'Y' | 'N';
    is_profitable?: 'Y' | 'N';
    prof_org_name?: string;
    is_prov_demo?: 'Y' | 'N';
    is_latest_year?: 'Y' | 'N';
    is_town_kinderg?: 'Y' | 'N';
    is_incl_kinderg?: 'Y' | 'N';
    is_affil_school?: 'Y' | 'N';
    affil_univ_code?: 'Y' | 'N';
    affil_univ_name?: 'Y' | 'N';
    is_last_yr_revok?: 'Y' | 'N';
    is_school_counted?: 'Y' | 'N';
  };
}
// 删除
export const deleteSchool = school_id => HttpSrv().delete(`/school/v1/school/?school_id=${school_id}`);
// 查询单个
export const getSchool = school_id => HttpSrv().get(`/school/v1/school/`, { params: { school_id } });
// 新增
export const createSchool = (params: CreateSchool) => HttpSrv().post('/school/v1/school/', params);
// 修改
export const updateSchool = (school_id, info: SchoolInfo) =>
  HttpSrv().put(`/school/v1/school/?school_id=${school_id}`, info);
// 查询学校
export const searchSchoolByName = (school_name: string, page = 1, per_page = 1000) =>
  HttpSrv().get(`/school/v1/school/search`, { params: { school_name, page, per_page } });

export interface SearchSchoolByNameParams {
  school_name?: string;
  school_id?: any;
  block?: string;
  borough?: string;
}

export const searchByNameSchool = (params: SearchSchoolByNameParams & PageParams) =>
  HttpSrv().get(`/school/v1/school/search`, { params });
// // 分页查询
// export const searchSchool = (params: SearchSchool, page: number, per_page: number) =>
//   HttpSrv().get('/school/v1/school/page', { params: { ...params, page, per_page } });
// 分页查询
export const searchSchool = (params: SearchSchool, page: number, per_page: number) => {
  const arr = [];
  if (params.founder_type) {
    params.founder_type.forEach(item => {
      arr.push(`founder_type=${item}`);
    });
    delete params.founder_type;
  }
  if (params.founder_type_lv2) {
    params.founder_type_lv2.forEach(item => {
      arr.push(`founder_type_lv2=${item}`);
    });
    delete params.founder_type_lv2;
  }
  if (params.founder_type_lv3) {
    params.founder_type_lv3.forEach(item => {
      arr.push(`founder_type_lv3=${item}`);
    });
    delete params.founder_type_lv3;
  }
  return HttpSrv().get(`/school/v1/school/page${arr && arr.length > 0 ? '?' : ''}${arr.join('&')}`, {
    params: { ...params, page, per_page },
  });
};
//todo: 这个不知道干嘛用的 /api/school/v1/school/baseinfo
// 关闭
export const closeSchool = (params: CloseSchool) =>
  HttpSrv().patch(`/school/v1/school/close`, {}, { params: { ...params } });
// 开办规划学校
export const openSchool = (school_id, info: SchoolInfo) =>
  HttpSrv().put(`/school/v1/school/open?school_id=${school_id}`, info);
// 关键信息修改
export const updateSchoolKeyInfo = (info: SchoolKeyInfo) => HttpSrv().put(`/school/v1/school/keyinfo`, info);

export const schoolCloseAudit = (process_instance_id: any, remark: string, is_approve: boolean) =>
  HttpSrv().patch(`/school/v1/school/close-audit`, {
    process_instance_id,
    remark,
    transaction_audit_action: is_approve ? 'pass' : 'refuse',
  });
export const schoolKeyinfoAudit = (process_instance_id: any, remark: string, is_approve: boolean) =>
  HttpSrv().patch(`/school/v1/school/keyinfo-audit`, {
    process_instance_id,
    remark,
    transaction_audit_action: is_approve ? 'pass' : 'refuse',
  });
export const schoolOpenAudit = (process_instance_id: any, remark: string, is_approve: boolean) =>
  HttpSrv().patch(`/school/v1/school/open-audit`, {
    process_instance_id,
    remark,
    transaction_audit_action: is_approve ? 'pass' : 'refuse',
  });

export const importSchool = (params: ImportParams) => HttpSrv().post(`/school/v1/school/school-import`, { params });

export const getSchoolAudit = (params: SearchSchool, page: number, per_page: number) => {
  const arr = [];
  if (params.founder_type) {
    params.founder_type.forEach(item => {
      arr.push(`founder_type=${item}`);
    });
    delete params.founder_type;
  }
  if (params.founder_type_lv2) {
    params.founder_type_lv2.forEach(item => {
      arr.push(`founder_type_lv2=${item}`);
    });
    delete params.founder_type_lv2;
  }
  if (params.founder_type_lv3) {
    params.founder_type_lv3.forEach(item => {
      arr.push(`founder_type_lv3=${item}`);
    });
    delete params.founder_type_lv3;
  }
  return HttpSrv().get(`/school/v1/school/school-audit${arr && arr.length > 0 ? '?' : ''}${arr.join('&')}`, {
    params: { ...params, page, per_page },
  });
};
